<?php
/**
 * @author qingyu8@staff.weibo.com
 * @date 2018/04/01
 */

namespace api\modules\v1\controllers;

use common\logic\Boat;
use common\logic\Occupancy;
use common\logic\Search;
use common\logic\Trip;
use common\models\BookerItem;
use Yii;
use yii\validators\Validator;
use yii\web\Controller;
use yii\web\Response;

class SearchController extends Controller {
    public function beforeAction($action) {
        Yii::$app->response->format = Response::FORMAT_JSON;
        return parent::beforeAction($action);
    }

    public function actionSuggest() {
        $r = \common\models\Boat::getList('', '*', 'id asc', 0, 8);
        $list = [];

        $priceMap = Occupancy::getBoatsPrice(array_column($r, 'id'));
        foreach ($r as $k => $v) {
            $gallery = json_decode($v['gallery_json'], true);
            $list[] = [
                "boatNameCN" => $v['name_cn'],
                "boatId" => $v['id'],
                "boatNameEN" => $v['name_en'],
                "locationName" => $v['countries_cn'],
                "imageUrl" => $gallery[0] ?? '',
                "price" => $priceMap[$v['id']]
            ];
        }
        return [
            'code' => 200,
            'data' => ['list' => $list]
        ];
    }

    public function actionGroup() {

    }

    public function actionBoat() {
        $get = Yii::$app->request->get();
        $list = [];
        $r = Search::search($get);
        if ($r['list']) {
            $priceMap = Occupancy::getBoatsPrice(array_column($r['list'], 'id'));
            $tripsMap = Trip::getTripsForSearch(array_column($r['list'], 'id'), Yii::$app->request->get('dateStart'), Yii::$app->request->get('endStart'));
            foreach ($r['list'] as $k => $v) {
                $gallery = $v['gallery_json'] ? json_decode($v['gallery_json'], true) : [];
                $images = [];
                foreach ($gallery as $g) {
                    $images[] = [
                        'imageUrl' => $g,
                    ];
                }
                $fs = json_decode($v['feature_json'], true);
                $fs['Onboard Facilities'] = empty($fs['Onboard Facilities']) ? [] : $fs['Onboard Facilities'];
                $str = '\'' . implode('\',\'', $fs['Onboard Facilities']) . '\'';
                if(empty($str)){
                    $feature=[];
                }else{
                    $feature = BookerItem::getColumn("name_en IN ($str)", 'name_cn');
                }
                $v['wifi'] != 'No' && $feature[]='Wifi';
                $v['nitrox'] != 'No' && $feature[]='高氧';

                $list[] = [
                    "boatId" => $v['id'],
                    "boatNameCN" => $v['name_cn'],
                    "boatNameEN" => $v['name_en'],
                    'countriesCN' => $v['countries_cn'],
                    'countriesEN' => $v['countries'],
                    "images" => $images,
                    //"level" => "豪华型",
                    "feature" => $feature,
                    "languages" => $v['languages_cn'],
                    //"rate" => 9.8,
                    "replyCount" => "1",
                    "price" => ['value'=>$priceMap[$v['id']],'unit'=>'￥'],
                    "trips" => $tripsMap[$v['id']],
                ];
            }
        }
        return [
            'code' => 200,
            'data' => [
                'list' => $list,
                'total' => (int)$r['total'],
                "page" => Yii::$app->request->get('page', 1),
            ]
        ];
    }
}
